1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package net.sf.deadbolt.handlers;
51
52 import java.util.Arrays;
53 import java.util.List;
54
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpServletResponse;
57
58 import net.sf.deadbolt.model.Room;
59
60 import org.apache.log4j.Logger;
61
62 /***
63 * This class is used to restrict or allow access to resources based on the user's
64 * IP address.
65 *
66 * @author Tim Solley <timsolley@yahoo.com>
67 */
68 public class IPFilterHandler extends DeadboltHandler {
69 private static Logger logger = Logger.getLogger(IPFilterHandler.class.getName());
70
71 /***
72 * This is the standard Deadbolt authenticate method. It will reject or allow
73 * access to resources based on what parameter and addresses are specified in
74 * the Deadbolt descriptor.
75 */
76 public boolean authenticate(HttpServletRequest request,
77 HttpServletResponse response, Room room) {
78 logger.debug("ENTERING: authenticate");
79
80
81 String remoteAddress = request.getRemoteAddr();
82 logger.debug("Remote IP address: " + remoteAddress);
83
84
85 String remoteHost = request.getRemoteHost();
86 logger.debug("Remote host: " + remoteAddress);
87
88
89
90 if(room.initParamExists("EXCLUDED-LIST") && room.initParamExists("INCLUDED-LIST")) {
91 logger.warn("Either EXCLUDED-LIST or INCLUDED-LIST should be specified, not both. Deadbolt will default to using the EXCLUDED-LIST only.");
92 }
93
94
95 if(room.initParamExists("EXCLUDED-LIST")) {
96 logger.debug("EXCLUDED-LIST was specified.");
97 String excludedAddressesParam = room.getInitParam("EXCLUDED-LIST");
98 List excludedAddresses;
99 String[] excludedAddressesArray = excludedAddressesParam.split(",");
100 for(int i = 0; i < excludedAddressesArray.length; i++) {
101 excludedAddressesArray[i] = excludedAddressesArray[i].trim();
102 }
103 excludedAddresses = Arrays.asList(excludedAddressesArray);
104
105 if(excludedAddresses.contains(remoteAddress) | excludedAddresses.contains(remoteHost)) {
106 logger.debug("The address is in the excluded list. Return false.");
107 addErrorKey(request, "ip.filter.handler.restricted");
108 return false;
109 } else {
110 logger.debug("The address is not in the excluded list. Return true.");
111 return true;
112 }
113 }
114
115
116 if(room.initParamExists("INCLUDED-LIST")) {
117 logger.debug("INCLUDED-LIST was specified.");
118 String includedAddressesParam = room.getInitParam("INCLUDED-LIST");
119 List includedAddresses;
120 String[] includedAddressesArray = includedAddressesParam.split(",");
121 for(int i = 0; i < includedAddressesArray.length; i++) {
122 includedAddressesArray[i] = includedAddressesArray[i].trim();
123 }
124 includedAddresses = Arrays.asList(includedAddressesArray);
125
126 if(includedAddresses.contains(remoteAddress) | includedAddresses.contains(remoteHost)) {
127 logger.debug("The address is in the included list. Return true.");
128 return true;
129 } else {
130 logger.debug("The address is not in the included list. Return false.");
131 addErrorKey(request, "ip.filter.handler.restricted");
132 return false;
133 }
134 }
135
136 logger.debug("EXITING: authenticate");
137 return false;
138 }
139
140 }